/*-----------------------------------------------------------------------------
-通用开发平台:基本组件的始类
-作者：张金军
-所有人：张金军
-版权：版权所有，张金军，2005-2015，保留所有权利
-----------------------------------------------------------------------------*/

Yacon.widget.InputIP = function(conText)
{
	conText = conText ||{};
	
	Yacon.apply(this,conText);
	
	this.create();
}
Yacon.widget.InputIP.Extend(Yacon.widget.WinControl,{
	attachElement:function(attachElement)
	{
		if (!attachElement)
			return;
		
		this.attach = this.setAttach(attachElement);
		if (!this.element&& !this.elementID)
			this.createTemplate();
		
		
		var fieldName      = this.attach.getAttribute("fieldName").trim();
		
		this.element.setAttribute("fieldName",fieldName);
		this.element.setAttribute("dataSet",this.attach.getAttribute("dataSet"));
		
		var attachPost   = Yacon.getAbsPosition(this.attach);
		
		this.element.style.setAttribute("left",attachPost[0]); 
		this.element.style.setAttribute("top",attachPost[1]);  
		this.element.style.setAttribute("width",attachPost[2]); 
		this.element.style.setAttribute("height",attachPost[3]-2);
		this.element.style.setAttribute("visibility","visible");  
		this.element.style.setAttribute("position","absolute");  
		
		this.initialization();
		if (this.ip1)
			this.ip1.focus();
	},
	create:function()
	{
		if (this.attach)
			this.attachElement(this.attach);
		
		if (!this.element)
			this.element   = document.getElementById(this.elementID);
		else
			this.elementID = this.element.getAttribute("id");
		
		if (!this.element)
			return;			
		
		
		this.initialization();
	},
	
	initialization:function()
	{
		this.fieldName       = this.element.getAttribute("fieldName");
		this.element.Control = this;
		this.elementType     = "inputIP";
		this.isSelected      = false;
		
		this.ip1  = document.getElementById(this.elementID+"_1");
		this.ip2  = document.getElementById(this.elementID+"_2");
		this.ip3  = document.getElementById(this.elementID+"_3");
		this.ip4  = document.getElementById(this.elementID+"_4");
		
		if (!this.ip1 ||!this.ip2||!this.ip3||!this.ip4)
			return;
		
		this.ip=[this.ip1,this.ip2,this.ip3,this.ip4];
		for(var i=0; i<this.ip.length;i++)
		{
			this.ip[i].Control = this; 
		}	
		
		this.setDataSet();
		this.setWidth(this.element.offsetWidth);
		if (Yacon.isTrue(this.element.getAttribute("fieldDisabled")))
			this.setDisabled(true);	
		else
			this.setDisabled();
	},
	setDataSet:function(dataSet)
	{
		if (!dataSet)
		{
			if (window.dataSet)
				this.dataSet = window.dataSet;
			else
			   	this.dataSet   =  getDatasetByID(this.element.getAttribute("dataSet"));
			
			if (this.dataSet)
			{
				this.dataSet.regElement(this);
				this.setPriFieldValue(this.dataSet.getFieldValue(this.fieldName));
				addElement(this.element);
			}	
			
		}else
		{
			this.dataSet = dataSet;
		}
	},
	setDisabled:function(disabled)
	{
		if (!Yacon.isEmpty(disabled))
		{
			this.element.disabled = disabled;
		}else
		{
			if (this.dataSet && this.dataSet.getRecord())
			{	
				if (this.dataSet.getRecord().getState().isEqual("insert"))
				{
					if(window.isIE8 || window.isIE9){
						this.element.disabled = Yacon.isTrue(this.element.insertDisabled);						
					}else{
						this.element.disabled = Yacon.isTrue(this.element.getAttribute("insertDisabled"));												
					}
				}else if (this.dataSet.getRecord().getState().isEqual("edit"))
				{
					if(window.isIE8 || window.isIE9){
						this.element.disabled = Yacon.isTrue(this.element.editDisabled);
					}else{
						this.element.disabled = Yacon.isTrue(this.element.getAttribute("editDisabled"));						
					}
				}else
				{
					this.element.disabled = true;
				}
			}
		}
		for(var i=0; i<this.ip.length;i++)
		{
			this.ip[i].setAttribute("disabled",this.element.disabled); 
			this.ip[i].style.setAttribute("imeMode","disabled"); 
		}	
	
	},
	setWidth:function(width)
	{
		width = Yacon.getInt(width);
		if (width < 120)
			return;
		width = (width-30)/4;
		for(var i=0; i<this.ip.length;i++)
			 this.ip[i].style.setAttribute("width",width); 		
    }, 
	setFieldValue:function(fieldValue)
	{
    	this.dataSet.setFieldValue(this.fieldName,fieldValue);
    },   
    setPriFieldValue:function(fieldValue,fieldText)
	{
		 fieldValue = fieldValue.replace(/[^\d.]/g,"");
		 if(fieldValue == "")
			 fieldValue = "..."
			
		 fieldValue = fieldValue.split(".");
		 
		 this.ip1.value =  this.getCheckValue(fieldValue[0]);
		 this.ip2.value =  this.getCheckValue(fieldValue[1]);
		 this.ip3.value =  this.getCheckValue(fieldValue[2]);
		 this.ip4.value =  this.getCheckValue(fieldValue[3]);
		
	 },
	 getFieldValue:function()
	 {
	 	if (this.dataSet)
	 		return this.dataSet.getFieldValue(this.fieldName);
	 	else
	 	{
	 		var ip=[this.ip1.value,this.ip2.value,this.ip3.value,this.ip4.value];
	 		return ip.join(".");
	 	}
	},
	 getCheckValue:function(value)
	 {
		 var value = Yacon.getInt(value);
		 if( value >= 0 && value <= 255 )
		 {
	        return value;
		 }else
		 {
			 return "";
	     }		 
	 },
	 ifCheckNull:function()
	 {
		 for(var i=0; i<this.ip.length;i++)
		 {
			 if (this.ip[i].value.trim() == "")
			 {
				 addNullElement(this.ip[i]);
			 }
		 }		 
	 },
	 createTemplate:function()
	 {
		if (!document.all("template_ip"))
		{	
			this.element = document.createElement("<span id='template_ip'   class='ipv4'  fieldType='IP' onmouseleave='Yacon.widget.InputIP.events.onTemplateBlur(this)'></span>");
			
			var templateStr = "";
			for(var i=1;i<5;i++)
			{
				if (i==1)
					templateStr += this.createIPUnit(i);
				else
					templateStr +="."+ this.createIPUnit(i);
			}			
			this.element.innerHTML = templateStr;
			document.body.appendChild(this.element);			
		}else
		{
			this.element =  document.all("template_ip");
		}
		this.elementID = "template_ip";
	 },		
	 createIPUnit:function(index)
	 {
		var unitTemplate = "<input class='ipinput' id='template_ip_"+index+"' maxlength='3' size='3' type='text'" +
		" onFocus='Yacon.widget.InputIP.events.onfocus(this)' onKeyUp='Yacon.widget.InputIP.events.onchange(this)' onChange='Yacon.widget.InputIP.events.onchange(this)' " +
		" onblur='Yacon.widget.InputIP.events.onBlur(this)' onKeyPress='Yacon.widget.InputIP.events.onkeypress(this)' onKeyDown='Yacon.widget.InputIP.events.onkeydown(this)'>";
		
		return unitTemplate;
	 }	
});


Yacon.widget.InputIP.events= {	
	
	 onTemplateBlur:function(obj){
		obj.style.setAttribute("visibility","hidden"); 
 	 },
 	 onBlur:function(obj){
 		 if (obj.Control)  
 			 obj.Control.isSelected = false; 
 		
  	 },
	 onfocus:function(obj){
    	 
 		 obj.select();
 		 if (obj.Control)  
 			 obj.Control.isSelected = true; 
	 },
	 onchange:function(obj)
	 {
		 var value = Yacon.getInt(obj.value);
		 if( value >= 0 && value <= 255 )
		 {
	        if( value!= obj.value)
	        {
	            obj.value = value;
	        }
		 }else
		 {
	         obj.value="";
	     }	
		 if (obj.Control)
		 {
			 var ip=[obj.Control.ip1.value,obj.Control.ip2.value,
			         obj.Control.ip3.value,obj.Control.ip4.value];
		    	
			 obj.Control.setFieldValue(ip.join("."));
		 }
	},
	 onkeypress:function(obj)
	 {
		 var key = event.charCode||event.keyCode;
		 var pos = Yacon.getSelection(obj);
		 var value = obj.value;
		 var c	=	String.fromCharCode(key);
		 
		 if(key>=48 && key<=57)
		 {
			 value=""+value.substring(0,pos.start)
			 	+ c + value.substring(pos.end,value.length);
        
			 if(parseInt(value)<255)
			 {
				 var id=obj.id;
				 /(.*)_(\d)$/.test(id);
				 var index=RegExp.$2;
				 IP_id=RegExp.$1;
				 
				 if(parseInt(value)>=100)
				 {
					 if(parseInt(index)<4)
					 {
						 id=id.replace(/(\d)$/,parseInt(index)+1 );
						 setTimeout("document.getElementById('"+id+"').focus();"+
								 "document.getElementById('"+id+"').select();",200);
					 }
				 }
				 return true;
			 } else
			 {
                 if(event.preventDefault)
                	 event.preventDefault();
                 
                 event.returnValue=false;
                 
                 return false;
			 }
		 } else{
			 if(event.preventDefault)
				 event.preventDefault();
			 event.returnValue=false;
		 }
	 },
	 
	 onkeydown:function(obj)
	 {
		 var key = event.charCode||event.keyCode;
		 var pos = Yacon.getSelection(obj);
		 var value = obj.value;
		 var c = String.fromCharCode(key);
		 var id = obj.id;
		 /^(.*)_(\d)$/.test(id);
		 
		 var index=RegExp.$2;
		 var Ip_Id=RegExp.$1;
		 
		 switch(key)
		 {
		 	/**
		 	 * 回车键
		 	 */
		 	case 13:
			{
				if(obj.getAttribute("nextid"))
				{
					document.all(obj.getAttribute("nextid")).focus();
				} else {
					 window.event.keyCode = 9;
				}
				break;
		 	
			}
			/**
			 * .小主键盘
			 */
			case 110:
		 	case 190:
		 	{
		 		if(index<4)
			 	{
			 		id=id.replace(/(\d)$/,parseInt(index)+1 );
			 		document.getElementById(id).focus();
			 		document.getElementById(id).select();
			 	}
			 	break;
		 	}
		 	/**
		 	 * up
		 	 */
		 	case 38:
		 	{
		 		value=!isNaN(parseInt(value))?parseInt(value):"";
		 		if(value=="")
		 			value=0;
		 		if(value<255)
		 		{
		 			obj.value=value+1;
		 		}
		 		else
		 			obj.value=0;
               break;
		 	}
		 	/**
		 	 * down
		 	 */	
		 	case 40:
		 	{
		 		value=!isNaN(parseInt(value))?parseInt(value):"";
    
		 		if(value=="")
		 			value=255;
		 		if(value>0)
		 		{
		 			obj.value=value-1;
		 		};
		 		break;
		 	}
		 	/**
		 	 *backspace 
		 	 */
		 	case 8:
		 	{
		 		if(pos.start>0)
		 			return;
        
		 	}
		 	/**
		 	 * left
		 	 */
		 	case 37:
		 	{
		 		if(pos.end==0 && index>1)
		 		{
		 			id=id.replace(/(\d)$/,parseInt(index)-1 );
		 			document.getElementById(id).focus();
		 			document.getElementById(id).select();
		 		}
		 		break;
		 	}
		 	/**
		 	 * right
		 	 */
		 	case 39:
		 	{
		 		if(pos.start == value.length && index < 4)
		 		{
		 			id=id.replace(/(\d)$/,parseInt(index)+1 );
		 			document.getElementById(id).focus();
		 			document.getElementById(id).select();
		 		}
		 		break;
		 	}
		 }
	 }
	
};

